import { defineStore } from "pinia";


interface UserInfo{
    id:number
    username:string
    email:string
    created_at:Date | string
}

interface State{
    currentUser:UserInfo | null
    dialogCount:number | null
}

export const useUsersStore = defineStore('users',{

    state:():State=>{

        let logUser;
        if(localStorage.getItem('userInfo')){
           logUser = JSON.parse(localStorage.getItem('userInfo') as string) 
        }
        return {
            currentUser:logUser,
            dialogCount:null
        }
    },

    getters:{
        getCurrentUser():UserInfo | string{
            if(this.currentUser){
                return this.currentUser
            }else{
                return '您当前还未登录'
            }
        }
    },

    actions:{
        UpdateCurrentUser(newCurrentUser:UserInfo){
            this.currentUser=newCurrentUser;
        }
    }
})